var hotel={
    label_room_name:"",
    label_room_type:"",
    label_room_price:"",
    currentRoomIndex:0,
    intervalName:null,
    create:function(contentId,link){
        callAjaxContent(contentId,link);
    },
   calcConveninceValue:function(){
      var sec_field = 'convenience';
      var _result = 0;
      for (var index=1; index < 19; index++){
         var _chkBox = dojo.byId('_'+sec_field+'_'+index+'_id');
         if (_chkBox.checked){
            _result= _result + Number(_chkBox.value);
         }
      }
      dojo.byId('_'+sec_field+'_id').value=_result;
   },
   search:function(form){
      var targetNode = null;
      var xhrArgs = {form:form,handleAs:"text",
          load: function(data) {
             dialogObj.setContent(data);
             dialogObj.show();
          },
          error: function(error) {
            var strError = "An unexpected error occurred: " + error;
            dialogObj.setContent(strError);
            dialogObj.show();
          }
      };
      dojo.xhrPost(xhrArgs);
   },
   closeRoom:function(){
      var slideTarget = dojo.byId("_div_hotel_roomInfo_");
      dojo.animateProperty({
          node: slideTarget,
              properties: {
              width: { start: 440, end: 100 },
              opacity: { start: 1, end: 0 }
          },
          duration: 800,
          onEnd:function(){
             hotel.nextRoom(hotel.currentRoomIndex);
          }
      }).play()
    },
    nextRoom:function(currentIndex){
       this.updateRoom(currentIndex);
       var slideTarget = dojo.byId("_div_hotel_roomInfo_");
       dojo.animateProperty({
           node: slideTarget,
               properties: {
               width: { start: 100, end: 440 },
               opacity: { start: 0, end: 1 }
           },
           duration: 800
       }).play();
    },
    updateRoom:function(currentIndex){
       var pTags = dojo.query('p','_div_hotel_roomInfo_');
       var room = roomObjects[currentIndex];
       pTags[0].innerHTML = '<label>'+this.label_room_name + ":</label>" + room.name;
       pTags[1].innerHTML = '<label>'+this.label_room_type + ":</label>" + room.type;
       pTags[2].innerHTML = '<label>'+this.label_room_price + ":</label>" + room.price;
    },
    startInitializeRoom:function(){
       this.currentRoomIndex = 0;
       this.clearInterval();
       document.getElementById('_room_slide_id').style.display='none';
       if (roomObjects.length == 1){
          this.updateRoom(this.currentRoomIndex);
       }else {
          this.setInterval();
       }
       this.updateRoom(this.currentRoomIndex);
    },
    clearInterval:function(){
       if (this.intervalName != null){
          window.clearInterval(this.intervalName);
          this.intervalName = null;
       }
    },
    setInterval:function(){
       this.intervalName = window.setInterval(function () {
          hotel.currentRoomIndex = hotel.currentRoomIndex + 1;
          if (hotel.currentRoomIndex >= roomObjects.length){
             hotel.currentRoomIndex = 0;
          }
          hotel.closeRoom();
       }, 5000);
    },
    handleViewRoom:function(evt){
       document.getElementById('_room_slide_id').style.display='';
       this.clearInterval();
       for(var i=0;i<roomObjects.length;i++){
          if (roomObjects[i].id == evt){
             this.nextRoom(i);
             return;
          }
       }
    },
    handleSlideClick:function(){
       this.nextRoom(this.currentRoomIndex);
       this.setInterval();
       document.getElementById('_room_slide_id').style.display='none';
    }
}
var foodstall={
   calcPeriodTimeValue:function(){
     var sec_field = 'periodTime';
     var _result = 0;
     for (var index=1; index < 4; index++){
        var _chkBox = dojo.byId('_'+sec_field+'_'+index+'_id');
        if (_chkBox.checked){
           _result= _result + Number(_chkBox.value);
        }
     }
     dojo.byId('_'+sec_field+'_id').value=_result;
  }
}

var _comment={
   showTextInputComment:function(){
      document.getElementById("button_InputComment").style.display="none";
      document.getElementById("text_InputComment").style.display="";
   },
   saveComment:function(attractionType,attractionMenu){
      callAjaxPost(null,"comment",null,true,_comment.saveCommentCallBack);
   },
   deleteComment:function(commentDiv,actionUrl){
      var commentForm = document.getElementById('commandDeleteCommentForm');
      commentForm.action=actionUrl;
      callAjaxPost(null,"commandDeleteCommentForm",null,true,_comment.deleleCommentCallBack, commentDiv);
   },
   saveCommentCallBack:function(){
      var commentAreaObj = document.getElementById('content_id');
      var commentContent = {content:commentAreaObj.value,participant:_participant}
      var template ='<div><div style="background-color:#695D47; float: left; margin: 0 12px 5px 0;">';
      template += '<a href="#"><img height="48px" src="{participant.avatarUrl}" alt="" title=""/></a></div>';
      template += '<h2><strong><a href="#">{participant.displayName}</a></strong></h2>';
      template += '<p>{content}</p></div><div class="clear"/><div class="separate"/>';
      dojo.create('div',{innerHTML:dojo.replace(template,commentContent)},dojo.query('#divCommentArea')[0],'first');
      commentAreaObj.value = "";
      document.getElementById("button_InputComment").style.display="";
      document.getElementById("text_InputComment").style.display="none";
   },
   deleleCommentCallBack:function(commentDiv){
      commentDiv.innerHTML = '<div style="font-style:italic; padding: 3px;">'+msgCommentDelete+'</div><div class="separate"/>';
   }
}

function gotoNextView(path, parentId){
   var targetNode = dojo.byId(parentId);
   var nextPageArea = dojo.query("div.nextPageArea", parentId)[0];
   var xhrArgs = {
       url: path,
       handleAs: "text",
       load: function(data) {
          dojo.destroy(nextPageArea);
          dojo.create("div", {innerHTML: data}, targetNode, "last");
       },
       error: function(error) {
         targetNode.innerHTML = "An unexpected error occurred: " + error;
       }
   };
   dojo.xhrGet(xhrArgs);
}
function confirmComment(currentObj,message,obj){
   var r=confirm(message)
   if (r==true){
      _comment.deleteComment(currentObj.parentNode.parentNode.parentNode,obj);
   }
   return false;
}

function callAjaxContent(contentId,ajaxUrl,callback){
   callAjaxNormal(contentId, ajaxUrl + "/light", callback);
};

function callAjaxNormal(contentId,ajaxUrl,callback){
   var targetNode = dojo.byId(contentId);
   var xhrArgs = {
       url: ajaxUrl,
       handleAs: "text",
       callback:callback,
       load: function(data) {
         dojo.html.set(targetNode,data);
         dojo.forEach(dojo.query('script',targetNode),function(node){
            dojo.eval(node.innerHTML);
         })
         if (this.callback){
            this.callback();
         }
       },
       error: function(error) {
         targetNode.innerHTML = "An unexpected error occurred: " + error;
       }
   };
   dojo.xhrGet(xhrArgs);
};

function callAjaxPost(contentId,form,callback,hasProcessStatus,successCallback){
   var targetNode = null;
   var currentArguments = arguments;
   if (contentId) targetNode=dojo.byId(contentId);
   var xhrArgs = { form:form, handleAs: "text", callback:callback,
       load: function(data) {
         if (targetNode && !hasProcessStatus){
            dojo.html.set(targetNode,data);
            dojo.forEach(dojo.query('script',targetNode),function(node){
               dojo.eval(node.innerHTML);
            })
         }
         if (hasProcessStatus && parseTextDataStatus(data) && successCallback){
            if (currentArguments.length > 5) successCallback(currentArguments[5]);
            else successCallback();
         }else if (this.callback){
            this.callback();
         }
       },
       error: function(error) {
         var strError = "An unexpected error occurred: " + error;
         if (targetNode) targetNode.innerHTML = strError;
         else alert(strError);
       }
   };
   dojo.xhrPost(xhrArgs);
};

function parseTextDataStatus(data){
   var indexOf=data.indexOf("##@");
   var lastIndexOf=data.lastIndexOf("##@");
   var error = 'Error';
   if (indexOf >= 0 && lastIndexOf > 0){
      var strStatus=data.substring(indexOf+3, lastIndexOf);
      var obj = dojo.fromJson(strStatus);
      if (obj.status == 'SUCC')
         return true;
      if (obj.status == 'FAIL')
         error = obj.error;
   }
   alert(error);
   return false;
}

var currentOpenWindow = null;
function openNewWindow(locationUrl){
   if (currentOpenWindow == null || currentOpenWindow.closed){
      currentOpenWindow = window.open(locationUrl);
   }else {
      currentOpenWindow.location.href = locationUrl;
   }
}